home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBCONV.C < prev    next >
Text File  |  1987-10-24  |  12KB  |  539 lines

  1.  
  2. /*
  3.  *  MBCONV.C - 9/19/87 Mail and User file conversion.
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. /*
  9.  *  Message header record.
  10.  */
  11.  
  12. #define o_m_read   0x01  /* Message has been read by recipient      */
  13. #define o_m_fwd    0x02  /* Message has been forwarded          */
  14.  
  15. #define o_mhtitl 80     /* Length of title. */
  16.  
  17. typedef struct o_m_s
  18. {
  19.   word next;          /* Next message header record             */
  20.   word prev;          /* Previous message header record          */
  21.   word ext;          /* Header extension record, or zero         */
  22.   word number;          /* Message number                  */
  23.   word size;          /* Size in bytes                     */
  24.   char type;          /* Message type                     */
  25.   byte stat;          /* Message status, see bit definitions above     */
  26.   char to  [ln_call]; /* Destination call                 */
  27.   char from[ln_call]; /* Originator call                 */
  28.   char date[ln_date]; /* Entry date                     */
  29.   char time[ln_time]; /* Entry time                     */
  30.   word rn;          /* Record number of this record             */
  31.   word read;          /* # times the message has been read         */
  32.   word unu3;          /* Available                     */
  33.   char bbs [ln_call]; /* Destination BBS, or distribution list         */
  34.   word unu2;          /* Available                     */
  35.   char title[o_mhtitl];
  36. } O_M;
  37.  
  38. /*
  39.  *  Message header extension item.
  40.  */
  41.  
  42. #define ommesn 17  /* Max calls in distribution list */
  43.  
  44. typedef struct o_m_ext_s
  45. {
  46.   word next;             /* Next extension item, or free item   */
  47.   char call[ommesn][ln_call]; /* Calls to send it to             */
  48.   byte flag[ommesn];          /* TRUE if need to send, FALSE if sent */
  49.   byte count;             /* Number of calls in list         */
  50.   word unu1;
  51.   word unu2;
  52.   word unu3;
  53. } O_M_EXT;
  54.  
  55. /*
  56.  *  Mail file header record.
  57.  */
  58.  
  59. typedef struct o_m_hdr_s
  60. {
  61.   word next;          /* Next record to allocate     */
  62.   word first;          /* First message header record     */
  63.   word last;          /* Last message header record     */
  64.   word ffree;          /* First record in free chain     */
  65.   word lfree;          /* Last record in free chain     */
  66.   word next_msg;      /* Next message number         */
  67.   byte version;       /* File format version number     */
  68.   word free;          /* Number of records in free chain */
  69.   word count;          /* Number of messages         */
  70.   char date[ln_date]; /* Date of last untangle         */
  71.   char time[ln_time]; /* Time of last untangle         */
  72.   word unt_msg;       /* next_msg at last untangle     */
  73.   char unused[99];
  74. } O_M_HDR;
  75.  
  76. /*
  77.  *  User file header record.
  78.  */
  79.  
  80. typedef struct o_u_hdr_s
  81. {
  82.   word count;          /* Number of users in file     */
  83.   char version;       /* File version             */
  84.   char date[ln_date]; /* Date of last compress         */
  85.   char time[ln_time]; /* Time of last compress         */
  86.   word lmnr;          /* Last message # at last compress */
  87.   char dirty;          /* True if anything changed     */
  88.   char wpdate[ln_date];
  89.   char free[106];     /* Ain't used at all, yet          */
  90. } O_U_HDR;
  91.  
  92. /*
  93.  *  User file user record.
  94.  */
  95.  
  96. /*
  97.  *  User record options.
  98.  */
  99.  
  100. #define o_u_local    0x01
  101. #define o_u_bbs      0x02
  102. #define o_u_expert   0x04
  103. #define o_u_delete   0x08
  104. #define o_u_sysop    0x10
  105. #define o_u_exclude  0x20
  106.  
  107. /*
  108.  *  User record state.
  109.  */
  110.  
  111. #define o_u_sent     0x01
  112. #define o_u_name     0x02
  113. #define o_u_home     0x04
  114.  
  115. #define o_pathl 81
  116.  
  117. typedef struct o_u_s
  118. {
  119.   char call[ln_call];      /* Users call              */
  120.   char date[ln_date];      /* Date of last logout         */
  121.   char time[ln_time];      /* Time of last logout         */
  122.   word msg_number;      /* Last message # at last login     */
  123.   byte ssid;          /* First character of ssid         */
  124.   byte state;          /* Got name? Got home? Sent to WP?     */
  125.   char handle[ln_handle]; /* Users name              */
  126.   byte options;       /* Permissions and stuff like that     */
  127.   char port;          /* Port user last connected, L if link */
  128.   char path[o_pathl];        /* NULL terminated string           */
  129.   word log_count;      /* Number of times user has connected  */
  130.   char home_bbs[ln_call]; /* Users home bbs             */
  131.   word rn;          /* Record number, zero if not valid     */
  132.   char unu2[3];
  133. } O_U;
  134.  
  135. char *mbfile, *mbbfile, *msgdir;
  136. int mfl, mflb;
  137.  
  138. char *ufwd, *bfwd;
  139. short ufwdm, ufwdn, bfwdm, bfwdn;
  140. char wpcall[ln_call];
  141.  
  142. /*
  143.  *  Prompts associated with message commands.
  144.  */
  145.  
  146. char *mm[num_mm];
  147.  
  148. MAIL_HDR *mfhs;
  149. MSG_HDR  *mmhs;
  150. MSG_EXT  *mmes;
  151.  
  152. char  *usfile, *usbfile;
  153. int    ufl, uflb;
  154. char tcall[ln_call];  /* Used all over as temp */
  155.  
  156. USER_HDR *ufhs;
  157. USER     *tuser;
  158.  
  159. char *um[num_um];
  160.  
  161.  
  162. main(argc, argv)
  163. int argc;
  164. char *argv[];
  165. {
  166.   convmsg();
  167.  
  168.   convuser();
  169. }
  170.  
  171. /*
  172.  *  Copy LJSF string to C string.
  173.  */
  174.  
  175. unbl(to, from, size)
  176. char *to, *from;
  177. int size;
  178. {
  179.   while (size--)
  180.   {
  181.     if (*from <= ' ') { *to = '\0'; return; }
  182.     *to++ = *from++;
  183.   }
  184.   *to = '\0';
  185. }
  186.  
  187. /*
  188.  *  Remove new line from end of string.
  189.  */
  190.  
  191. remnl(p)
  192. char *p;
  193. {
  194.   for (; *p; p++) if (*p is '\n') { *p = '\0'; return; }
  195. }
  196.  
  197. /*
  198.  *  Fill some memory with a character.
  199.  */
  200.  
  201. fill(adr, ch, len)
  202. char *adr;
  203. char ch;
  204. int len;
  205. {
  206.   while (len--) *adr++ = ch;
  207. }
  208.  
  209. convmsg()
  210. {
  211.   register word i;
  212.   register char *p;
  213.  
  214.   O_M_HDR  *omfhs;
  215.   O_M       *ommhs;
  216.   O_M_EXT  *ommes;
  217.  
  218.   O_U_HDR  *oufhs;
  219.   O_U       *otuser;
  220.  
  221. /*
  222.  *  Allocate space for the mail file records.
  223.  */
  224.  
  225.   omfhs = (O_M_HDR *)  malloc(sizeof(O_M_HDR));
  226.   ommhs = (O_M *)      malloc(sizeof(O_M));
  227.   ommes = (O_M_EXT *)  malloc(sizeof(O_M_EXT));
  228.  
  229.   mfhs    = (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
  230.   mmhs    = (MSG_HDR *)  malloc(sizeof(MSG_HDR));
  231.   mmes    = (MSG_EXT *)  malloc(sizeof(MSG_EXT));
  232.  
  233.   if ((mflb = open("MAIL.DAT", O_RDONLY | O_BINARY)) < 0)
  234.   {
  235.     printf("MAIL.DAT not found.\n");
  236.     return;
  237.   }
  238.  
  239. /*
  240.  *  Read the mail file header.
  241.  */
  242.  
  243.   read128(mflb, 0, (char *)omfhs);
  244.  
  245.   if ((omfhs->version < 1) or (omfhs->version > 7))
  246.   {
  247.     printf("MAIL.DAT is version %d, unrecognized version.\n", omfhs->version);
  248.     return;
  249.   }
  250.  
  251.   printf("Creating the new message file MAIL.NEW\n");
  252.  
  253.   mfl = open("MAIL.NEW", O_CREAT | O_RDWR | O_BINARY, pmode);
  254.  
  255.   printf("%d messages in MAIL.DAT\n", omfhs->count);
  256.  
  257. /*
  258.  *  Convert the file header record.
  259.  */
  260.  
  261.   fill(mfhs, '\0', 256);
  262.  
  263.   mfhs->next     = omfhs->next;
  264.   mfhs->first     = omfhs->first;
  265.   mfhs->last     = omfhs->last;
  266.   mfhs->ffree     = omfhs->ffree;
  267.   mfhs->lfree     = omfhs->lfree;
  268.   mfhs->next_msg = omfhs->next_msg;
  269.   mfhs->version  = mb_version;
  270.   mfhs->free     = omfhs->free;
  271.   mfhs->count     = omfhs->count;
  272.   mfhs->unt_msg  = omfhs->unt_msg;
  273.  
  274.   strncpy(mfhs->date, omfhs->date, ln_date);
  275.   strncpy(mfhs->time, omfhs->time, ln_time);
  276.  
  277. /*
  278.  *  Write the V8 file header.
  279.  */
  280.  
  281.   write256(mfl, 0, (char *)mfhs);
  282.  
  283. /*
  284.  *  Convert the free record chain (lazy ...)
  285.  */
  286.  
  287.   for (i = omfhs->ffree; i; i = ommhs->next)
  288.   {
  289.     read128(mflb, i, (char *)ommhs);
  290.     fill(mmhs, '\0', 256);
  291.     mmhs->next = ommhs->next;
  292.     write256(mfl, i, (char *)mmhs);
  293.   }
  294.  
  295. /*
  296.  *  Convert the message headers.
  297.  */
  298.  
  299.   for (i = omfhs->first; i; i = ommhs->next)
  300.   {
  301.     read128(mflb, i, (char *)ommhs);
  302.     printf("%5u %5u %5u %6.6s %6.6s\n",
  303.       i, ommhs->rn, ommhs->number, ommhs->to, ommhs->from);
  304. /*
  305.  *  First bring the old message header record up to V 7.
  306.  */
  307.  
  308.     if (omfhs->version < 3) remnl(ommhs->title);
  309.  
  310.     if (omfhs->version < 4)
  311.     {
  312.       ommhs->ext = 0;
  313.       switch (ommhs->stat)
  314.       {
  315.     case 'F' : ommhs->stat = m_fwd;  break;
  316.     case 'N' : ommhs->stat = 0;      break;
  317.     case 'Y' : ommhs->stat = m_read; break;
  318.       }
  319.     }
  320.  
  321.     if (omfhs->version < 6)
  322.     {
  323.       ommhs->next = ommhs->ext;
  324.       ommhs->prev = ommhs->unu3;
  325.       ommhs->ext  = ommhs->unu2;
  326.     }
  327.  
  328.     if (omfhs->version < 7) ommhs->rn = i;
  329.  
  330. /*
  331.  *  Now build the V8 record.
  332.  */
  333.  
  334.     fill(mmhs, '\0', 256);
  335.  
  336.     mmhs->next     = ommhs->next;
  337.     mmhs->prev     = ommhs->prev;
  338.     mmhs->ext     = ommhs->ext;
  339.     mmhs->number = ommhs->number;
  340.     mmhs->size     = ommhs->size;
  341.     mmhs->type     = ommhs->type;
  342.     mmhs->stat     = ommhs->stat;
  343.     mmhs->rn     = ommhs->rn;
  344.     mmhs->read     = ommhs->read;
  345.  
  346.     strncpy(mmhs->to,       ommhs->to,    ln_call);
  347.     strncpy(mmhs->from,    ommhs->from, ln_call);
  348.     strncpy(mmhs->bbs,       ommhs->bbs,    ln_call);
  349.     strncpy(mmhs->date,  ommhs->date, ln_date);
  350.     strncpy(mmhs->time,  ommhs->time, ln_time);
  351.  
  352.     fill(mmhs->bid, ' ', 12);
  353.  
  354.     strcpy(mmhs->title, ommhs->title);
  355.  
  356. /*
  357.  *  Write the new record in the new file.
  358.  */
  359.  
  360.     write256(mfl, i, (char *)mmhs);
  361.  
  362. /*
  363.  *  Convert the header extension record, if there is one.
  364.  */
  365.  
  366.     if (mmhs->ext)
  367.     {
  368.       read128(mflb, mmhs->ext, (char *)ommes);
  369.       mmes->next = ommes->next;         /* Next extension item, or free item   */
  370.       for (i = 0; i < ommes->count; i++)
  371.       {
  372.     strncpy(mmes->call[i], ommes->call[i], ln_call); /* Calls to send it to         */
  373.     mmes->flag[i] = ommes->flag[i];      /* TRUE if need to send, FALSE if sent */
  374.       }
  375.       mmes->count = ommes->count;         /* Number of calls in list        */
  376.       write256(mfl, mmhs->ext, (char *)mmes);
  377.     }
  378.   }
  379.   close (mfl);
  380.   close (mflb);
  381.  
  382.  
  383.   printf("Deleting the old mail file MAIL.DAT\n");
  384.   unlink("MAIL.DAT");
  385.   printf("Renaming MAIL.NEW to MAIL.DAT\n");
  386.   rename("MAIL.NEW", "MAIL.DAT");
  387. }
  388.  
  389. /*
  390.  *  Now do the user file.
  391.  */
  392.  
  393. convuser()
  394. {
  395.   register int i;
  396.  
  397.   O_U_HDR  *oufhs;
  398.   O_U       *otuser;
  399.  
  400. /*
  401.  *  Allocate space for the user file records.
  402.  */
  403.  
  404.   oufhs  = (O_U_HDR *)    malloc(sizeof(O_U_HDR));
  405.   otuser = (O_U *)    malloc(sizeof(O_U));
  406.  
  407.   ufhs     = (USER_HDR *) malloc(sizeof(USER_HDR));
  408.   tuser  = (USER *)    malloc(sizeof(USER));
  409.  
  410.   if ((uflb = open ("USER.DAT", O_RDONLY | O_BINARY)) < 0)
  411.   {
  412.     printf("USER.DAT not found.\n");
  413.     return;
  414.   }
  415.  
  416. /*
  417.  *  Read the old user file header.
  418.  */
  419.  
  420.   read128(uflb, 0, (char *)oufhs);
  421.   if ((oufhs->version < 1) or (oufhs->version > 7))
  422.   {
  423.     printf("USER.DAT is version %d, unrecognized version.\n", oufhs->version);
  424.     return;
  425.   }
  426.  
  427.   printf("%d users in USER.DAT\n", oufhs->count);
  428.  
  429.   printf("Creating the new user file USER.NEW\n");
  430.  
  431.   ufl = open("USER.NEW", O_CREAT | O_RDWR | O_BINARY, pmode);
  432.  
  433. /*
  434.  *  Update the file header to V8
  435.  */
  436.  
  437.   fill(ufhs, '\0', 256);
  438.  
  439.   ufhs->count    = oufhs->count;
  440.   ufhs->version = us_version;
  441.   ufhs->lmnr    = oufhs->lmnr;
  442.   ufhs->dirty    = false;
  443.  
  444.   strncpy(ufhs->date,    oufhs->date,   ln_date);
  445.   strncpy(ufhs->time,    oufhs->time,   ln_time);
  446.   strncpy(ufhs->wpdate, oufhs->wpdate, ln_date);
  447.  
  448. /*
  449.  *  Write the new file header.
  450.  */
  451.  
  452.   write256(ufl, 0, (char *)ufhs);
  453.  
  454. /*
  455.  *  Read each user record and update.
  456.  */
  457.  
  458.   for (i = 1; i <= oufhs->count; i++)
  459.   {
  460.     read128(uflb, i, (char *)otuser);
  461.  
  462. /*
  463.  *  First bring the record up to V8
  464.  */
  465.  
  466.     if (oufhs->version < 6) remnl(otuser->path);
  467.     if (oufhs->version < 7) otuser->ssid = otuser->ssid - '0';
  468.  
  469. /*
  470.  *  Build the V8 user record.
  471.  */
  472.  
  473.   fill(tuser, '\0', 256);
  474.  
  475.   tuser->msg_number = otuser->msg_number;
  476.   tuser->ssid        = otuser->ssid;
  477.   tuser->state        = otuser->state;
  478.   tuser->options    = otuser->options;
  479.   tuser->port        = otuser->port;
  480.   tuser->log_count  = otuser->log_count;
  481.   tuser->rn        = otuser->rn;
  482.  
  483.   strncpy(tuser->call,       otuser->call,     ln_call);
  484.   strncpy(tuser->handle,   otuser->handle,   ln_handle);
  485.   strncpy(tuser->home_bbs, otuser->home_bbs, ln_call);
  486.   strncpy(tuser->date, otuser->date, ln_date);
  487.   strncpy(tuser->time, otuser->time, ln_time);
  488.  
  489.   strcpy(tuser->path,  otuser->path);
  490.  
  491. /*
  492.  *  Write the V8 record to the new user file.
  493.  */
  494.     write256(ufl, i, (char *)tuser);
  495.   }
  496.  
  497.   close (ufl);
  498.   close (uflb);
  499.  
  500.   printf("Deleting the old user file USER.DAT\n");
  501.   unlink("USER.DAT");
  502.   printf("Renaming USER.NEW to USER.DAT\n");
  503.   rename("USER.NEW", "USER.DAT");
  504. }
  505.  
  506. /*
  507.  *  Read random 128 byte record.
  508.  */
  509.  
  510. read128(fid, rec, buf)
  511. int  fid;
  512. word rec;
  513. char *buf;
  514. {
  515.   long lseek();
  516.   long offs;
  517.  
  518.   offs = (long)rec * 128l;
  519.   lseek(fid, offs, 0);
  520.   return (read(fid, buf, 128) is 128);
  521. }
  522.  
  523. /*
  524.  *  Write random 256 byte record.
  525.  */
  526.  
  527. write256(fid, rec, buf)
  528. int  fid;
  529. word rec;
  530. char *buf;
  531. {
  532.   long lseek();
  533.   long offs;
  534.  
  535.   offs = (long)rec * 256l;
  536.   lseek(fid, offs, 0);
  537.   return (write(fid, buf, 256) is 256);
  538. }
  539.